package ym.controller.back.repair;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import ym.pojo.RepairEvaluation;
import ym.service.RepairEvaluationService;
import ym.vo.BaseResult;

import java.util.List;

/**
 * 后台维修评价
 */
@RestController
@CrossOrigin
@RequestMapping("/back/repairEvaluation")
public class RepairEvaluationController {
    @Autowired
    private RepairEvaluationService repairEvaluationService;

    /**
     * 新增
     *
     * @param repairEvaluation
     * @return
     */
    @PostMapping("/add")
    @PreAuthorize("hasAnyAuthority('/repair')")
    public BaseResult add(RepairEvaluation repairEvaluation) {
        repairEvaluationService.add(repairEvaluation);
        return BaseResult.ok();
    }

    /**
     * 修改
     *
     * @param repairEvaluation
     * @return
     */
    @PostMapping("/update")
    public BaseResult update(RepairEvaluation repairEvaluation) {
        repairEvaluationService.update(repairEvaluation);
        return BaseResult.ok();
    }

    /**
     * 删除
     *
     * @param repairEvaluationId
     * @return
     */
    @DeleteMapping("/delete")
    public BaseResult delete(Integer repairEvaluationId) {
        repairEvaluationService.delete(repairEvaluationId);
        return BaseResult.ok();
    }

    /**
     * 根据id查询
     *
     * @param repairEvaluationId
     * @return
     */
    @GetMapping("/findById")
    public BaseResult findById(Integer repairEvaluationId) {
        RepairEvaluation repairEvaluation = repairEvaluationService.findById(repairEvaluationId);
        return BaseResult.ok(repairEvaluation);
    }

    /**
     * 查询所有
     *
     * @return
     */
    @GetMapping("/findAll")
    public BaseResult findAll() {
        List<RepairEvaluation> repairEvaluations = repairEvaluationService.findAll();
        return BaseResult.ok(repairEvaluations);
    }

    /**
     * 分页查询
     *
     * @param page
     * @param size
     * @return
     */
    @PreAuthorize("hasAnyAuthority('/repair')")
    @GetMapping("/search")
    public BaseResult search(String search, int page, int size) {
        Page<RepairEvaluation> repairEvaluationPage = repairEvaluationService.search(search, page, size);
        return BaseResult.ok(repairEvaluationPage);
    }

    /**
     * 根据维修报备id查询
     *
     * @param repairReportId
     * @param search
     * @param page
     * @param size
     * @return
     */
    @PreAuthorize("hasAnyAuthority('/repair')")
    @GetMapping("/searchByRepairReportId")
    public BaseResult searchByRepairReportId(Integer repairReportId, String search, int page, int size) {
        Page<RepairEvaluation> repairEvaluationPage = repairEvaluationService.searchByRepairReportId(repairReportId, search, page, size);
        return BaseResult.ok(repairEvaluationPage);
    }
}
